package cn.com.zhuxulin.info.Utils;

import java.io.IOException;

import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class Cities extends DefaultHandler {
	private String[] cities = new String[200];
	private String cityURL = "http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx/getSupportCityString?theRegionCode=";

	public Cities(String proviceID) {
		// cities=new String[100];
		cityURL += proviceID;
	}

	public String[] getCities() throws ParserConfigurationException, SAXException, FactoryConfigurationError, IOException {
		SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
		parser.parse(HttpRequest.doGetForInputStream(cityURL), this);
		return cities;
	}

	// private class ParseCities extends DefaultHandler{
	private boolean flag = false;
	private int point = -1;

	public void characters(char[] ch, int start, int length) throws SAXException {
		if (flag) {
//			System.out.println(point);
			cities[point] = new String(ch, 0, length);
			flag = false;
		}
	}

	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
		if (qName.equals("string")) {
			flag = true;
			point++;
		}
	}
	// }
}
